package en.edu.sdjzu.xg.eagle.hairsalon.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.RoundingMode;
import java.util.UUID;
import java.math.BigDecimal;

@RestController
@RequestMapping("/pay")
public interface PayController {
    private static final String APP_ID = "your_app_id";
    private static final String APP_PRIVATE_KEY = "your_app_private_key";
    private static final String ALIPAY_PUBLIC_KEY = "alipay_public_key";
    private static final String NOTIFY_URL = "http://your-domain/pay/notify";

    @PostMapping("/recharge")
    public default <AlipayClient> String recharge(@RequestParam("amount") BigDecimal amount,
                                                  @RequestParam("userId") String userId) throws Exception {
        // 创建支付宝客户端
        AlipayClient alipayClient = (AlipayClient) new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                APP_ID,
                APP_PRIVATE_KEY,
                "json",
                "UTF-8",
                ALIPAY_PUBLIC_KEY,
                "RSA2"
        );

        // 构建支付请求
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl(NOTIFY_URL); // 支付成功跳转地址
        request.setNotifyUrl(NOTIFY_URL); // 异步通知地址

        // 支付参数
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(UUID.randomUUID().toString()); // 商户订单号
        model.setTotalAmount(amount.setScale(2, RoundingMode.HALF_UP).toString()); // 金额（元）
        model.setSubject("用户充值"); // 订单标题
        model.setProductCode("FAST_INSTANT_TRADE_PAY"); // 销售产品码

        request.setBizModel(model);

        // 生成支付链接（跳转支付宝收银台）
        return alipayClient.pageExecute(request).getBody();
    }
}
